package com.changgou.goods.service.impl;

import com.changgou.entity.PageResult;
import com.changgou.goods.dao.BrandMapper;
import com.changgou.goods.pojo.Brand;
import com.changgou.goods.service.BrandService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * 品牌
 * fxq
 */
@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    private BrandMapper brandMapper;
    /**
     * 添加品牌
     * @param brand
     */
    @Override
    public void add(Brand brand) {
        brandMapper.insertSelective(brand);
    }

    /**
     * 分页条件查询品牌列表
     * @return
     * fxq
     * @param page
     * @param size
     * @param condition
     */
    @Override
    public PageResult<Brand> findPage(int page, int size, String condition) {
        //设置分页
        PageHelper.startPage(page,size);
        //创建Example对象
        Example example = new Example(Brand.class);
        //创建Criteria对象
        Example.Criteria criteria = example.createCriteria();
        //拼接条件
        if(condition != null && !"".equals(condition)){
            criteria.orLike("name","%"+condition+"%");
            criteria.orLike("letter","%"+condition+"%");
        }
        //调用方法
        Page<Brand> pageLists = (Page<Brand>) brandMapper.selectByExample(example);
        //创建PageResult集合
        PageResult<Brand> pageInfo = new PageResult<>(pageLists.getTotal(),pageLists.getResult());
        return pageInfo;
    }

    /**
     * 删除品牌
     * @param id
     */
    @Override
    public void delete(Integer id){
        brandMapper.deleteByPrimaryKey(id);
    }


    /**
     * 根据商品分类id查询品牌列表
     * @param categoryId
     * @return
     */
    @Override
    public List<String> findBrandName(int categoryId) {
        return brandMapper.findBrandName(categoryId);
    }
}
